package com.dt.app.common.service.impl;

import com.dt.app.common.constant.ConstantCode;
import com.dt.app.common.service.AuthenticationInfoService;
import com.dt.app.modules.sys.entities.SysUserEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * 获取Security登录用户信息
 * @author DT
 * @date 2021/7/10 15:37
 */
@Service
public class AuthenticationInfoServiceImpl implements AuthenticationInfoService {

    @Override
    public Object getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getPrincipal();
    }
    public Integer getSysCompanyId(){
        try{
            SysUserEntity sysUserEntity = (SysUserEntity) getAuthentication();
            return sysUserEntity.getSysCompanyId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return -110;
    }
    public String getRoleName(){
        try{
            SysUserEntity sysUserEntity = (SysUserEntity) getAuthentication();
            return sysUserEntity.getRoleName();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "错误";
    }
    public boolean getIsAdmin(){
        try{
            return ConstantCode.ADMIN_ROLE_NAME.equals(this.getRoleName());
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
